package com.intelligentmeterreadingsys.report.activity;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.intelligentmeterreadingsys.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class MyVideoActivity extends Activity implements OnClickListener{

	public Button button1;//播放
	
	public Button button2;//暂停
	
	public Button button3;//重录
	
 private static final int REQUEST_CODE_TAKE_VIDEO = 2;
	
	public VideoView video;
	
	public String waternumber;//水表编号
	
	public String path;
	
	public Button button4;//返回
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myvideo);
		Intent intent=getIntent();
		String key=intent.getStringExtra("video");
		waternumber=intent.getStringExtra("waternumber");
		init();
		if(key.equals("ok")){
			System.out.println("ok");
			videoMethod();
		}else {
			System.out.println("播放");
			videoMethod();
		}
	}

	
	public void init(){
		button1=(Button)findViewById(R.id.video_play_button);
		button2=(Button)findViewById(R.id.video_pause_button);
		button3=(Button)findViewById(R.id.video_re_button);
		button4=(Button)findViewById(R.id.video_return_button);
		button4.setOnClickListener(this);
		video=(VideoView)findViewById(R.id.video_Myvideo);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		if(button1.getId()==v.getId()){
			video.start();
		}else if(button4.getId()==v.getId()){
			System.out.println("返回主界面");
			Intent intent=new Intent(MyVideoActivity.this, ReportAddActivity.class);
			intent.putExtra("videopath", path);
		    setResult(RESULT_OK, intent);
		    finish();
		}
		
	}
	 
	/**
	 * 装视频
	 */
	public void playVideo(String path){
		video.setVideoPath(path);  
		video.setMediaController(new MediaController(MyVideoActivity.this));  
		video.requestFocus();  

	}
	
	 //拍摄视频
	  private void videoMethod(){
		  Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
		   String   videopath = Environment.getExternalStorageDirectory().toString() + "/DownFile/Video/";
		             
		              String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".avi";
		              File out = new File(videopath);
		              if (!out.exists()) {
		                      out.mkdirs();
		              }
		              out = new File(videopath, fileName);
		              videopath =path= videopath + fileName;
		              Uri uri = Uri.fromFile(out);
		              intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		              intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
		    
		  startActivityForResult(intent,REQUEST_CODE_TAKE_VIDEO);
	  }


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		  if(requestCode==REQUEST_CODE_TAKE_VIDEO){
			  playVideo(path);
			  System.out.println(data.getData().toString());
		  }
		super.onActivityResult(requestCode, resultCode, data);
	}
	  
	  
}
